// 导入http
import './utils/http.js'
import './utils/util.js'
//导入个推
// import GtPush from './libs/gtpush-min.js'
const UpdateManager = wx.getUpdateManager()
App({
  count: 0, //tabbar角标
  userLocation: wx.getStorageSync('userLocation') || {},
  // 小程序启动的时候触发一次
  onLaunch() {
    this.getUserId()
    // 获得cid
    // this.gtConnect()
    if (wx.getStorageSync('userId')) {
      this.wsConnect(wx.getStorageSync('userId'))
    }
  },
  getUserId() {
    // 读取本地存储userId，设置给this
    this.userId = wx.getStorageSync('userId');
    // 读取本地存储openid，设置给this
    this.openid = wx.getStorageSync('openid');
    // 读取本地存储access_token，设置给this
    this.access_token = wx.getStorageSync('access_token');
    // 读取本地存储cid，设置给this
    this.cid = wx.getStorageSync('cid');
  },
  // setUserId方法，支持传入键和值
  setUserId(key, data) {
    this[key] = data
    // 存入本地
    wx.setStorageSync(key, data)
  },
  //连接webSocket
  wsConnect(userId) {
    wx.connectSocket({
      url: `wss://zhenpufangwu.cn:9000/utopia/ws?userId=${userId}`,
    })
    wx.onSocketOpen((res) => {})
    wx.onSocketClose((res) => {
      wx.connectSocket({
        url: `wss://zhenpufangwu.cn:9000/utopia/ws?userId=${userId}`,
      })
    })
    wx.onSocketError((res) => {})
  },
  //连接个推
  gtConnect(userId) {
    GtPush.enableSocket(true)
    GtPush.init({
      appid: 'FyRFIRkddTAAIm2nhssbyA',
      onClientId: async (res) => {
        //个推ClientId回调，拿到cid后将其上报到服务端
        console.info(`onClientId: ${res.cid}`)
        this.setUserId('cid', res.cid)
      },

      onlineState: (data) => {
        //个推ClientId在线状态回调
        console.info(`onlineState: ${data.online}`)
      },

      onPushMsg: (res) => {
        const msg = JSON.parse(res.message)
        msg.content = JSON.parse(msg.content)
        //推送消息回调
        console.info(msg)
        let notifyData = {}
        let notifyFlag = {}
        if (!!wx.getStorageSync('notifyData')) {
          notifyData = wx.getStorageSync('notifyData')
          notifyFlag = wx.getStorageSync('notifyFlag')
        } else {
          notifyData = {
            systemList: [],
            likeList: [],
            collectList: [],
            rewardList: [],
            commentList: []
          }
          notifyFlag = {
            allFlag: false,
            systemFlag: false,
            likeFlag: false,
            collectFlag: false,
            rewardFlag: false,
            commentFlag: false
          }
        }
        if (msg.notifyType === '1') {
          notifyData.systemList.unshift(msg)
          notifyFlag.systemFlag = true
          wx.showToast({
            title: msg.content.content,
            icon: 'none',
            duration: 2000
          })
        } else if (msg.notifyType === '2') {
          notifyData.likeList.unshift(msg)
          notifyFlag.likeFlag = true
        } else if (msg.notifyType === '3') {
          notifyData.collectList.unshift(msg)
          notifyFlag.collectFlag = true
        } else if (msg.notifyType === '4') {
          notifyData.rewardList.unshift(msg)
          notifyFlag.rewardFlag = true
        } else if (msg.notifyType === '6') {
          notifyData.commentList.unshift(msg)
          notifyFlag.commentFlag = true
        } else {
          return
        }
        notifyFlag.allFlag = true
        wx.setStorageSync('notifyData', notifyData)
        wx.setStorageSync('notifyFlag', notifyFlag)
      },

      onError: (res) => {
        //错误回调
        console.info(`错误`, res.error)
      }
    })
  },
  onUnload() {
    //关闭webSocket
    wx.closeSocket({
      success: () => {}
    })
    //关闭个推
  },
  globalData: {
    userInfo: null
  },
  // 退出登录
  loginOut() {
    // 清除本地
    wx.clearStorageSync()
    // 清除缓存
    UpdateManager.applyUpdate()
    // 清除app内的存储
    this.userId = ''
    this.userInfo = ''
    this.openid = ''
    this.access_token = ''
    //关闭webSocket
    wx.closeSocket({
      success: () => {
      }
    })
    // 跳转登录，清除所有页面栈
    wx.reLaunch({
      url: '/pkgUser/pages/login/login',
    })
  },
})